package com.future.myjavatools.core.zhoupu;

/**
 * @author lizuju
 * @date 2017/11/22
 * @remark
 */
public class LocationUtil {

    private static final double EARTH_RADIUS = 6378137;

    /**
     * 根据经纬度计算两地的距离
     *
     * @param pointA A点 <latitude,longitude>
     * @param pointB B点 <latitude,longitude>
     * @return
     */
    public static double getDistance(Pair<Double, Double> pointA, Pair<Double, Double> pointB) {
        double radLat1 = rad(pointA.getFirst());
        double radLng1 = rad(pointA.getSecond());
        double radLat2 = rad(pointB.getFirst());
        double radLng2 = rad(pointB.getSecond());
        double a = radLat1 - radLat2;
        double b = radLng1 - radLng2;
        double result = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2))) * EARTH_RADIUS;
        return result;
    }

    private static double rad(double d) {
        return d * Math.PI / 180d;
    }

    public static boolean checkLongitudeAndLatitude(double longitude, double latitude) {
        if (longitude > 180 || longitude < -180 || latitude > 90 || latitude < -90) {
            return false;
        }
        return true;
    }
}
